package ctrl_combat;

import habitant.Soldat;
import habitant.SoldatManager;
import habitant.StrategieImpossibleException;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import batiment.BatimentIntrouvableException;
import batiment.BatimentManager;

import royaume.RoyaumeImpl;
import royaume.RoyaumeIntrouvableException;
import royaume.RoyaumeManager;

/**
 * Servlet implementation class ServletAttaquer
 */
public class ServletAttaquer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RoyaumeImpl royaume;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletAttaquer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		royaume = (RoyaumeImpl) request.getSession().getAttribute("royaume");
		if (royaume == null) {
			System.out.println("Session royaume null");
			request.setAttribute("content", "");
			getServletContext().getRequestDispatcher("/ServletAccueil")
			.forward(request, response);
		} else {
			request.setAttribute("listeAttaquables", RoyaumeManager.getAttaquables(royaume));
			request.setAttribute("content", "attaquer.jsp");
			getServletContext().getRequestDispatcher("/ServletAccueil").forward(
					request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		royaume = (RoyaumeImpl) request.getSession().getAttribute("royaume");
		if (royaume == null) {
			System.out.println("Session royaume null");
			request.setAttribute("content", "");
			getServletContext().getRequestDispatcher("/ServletAccueil")
			.forward(request, response);
		} else {
			try{
				RoyaumeImpl cible = RoyaumeManager.getRoyaume(request.getParameter("att"));
				System.out.println("lancement de l'attaque : "+cible.get_nom());
				request.setAttribute("confirm",royaume.attaquer(cible, request.getParameter("choice"), BatimentManager.get(request.getParameter("destruction"))));
				request.setAttribute("content", "attaquer.jsp");
				getServletContext().getRequestDispatcher("/ServletAccueil")
				.forward(request, response);
			} catch (RoyaumeIntrouvableException e){
				e.printStackTrace();
			} catch (BatimentIntrouvableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
